//
//  CarouselImageView.m
//  UI07_轮播图
//
//  Created by dllo on 15/12/14.
//  Copyright © 2015年 蓝鸥科技.李贺. All rights reserved.
//

#import "CarouselImageView.h"
#import <UIImageView+WebCache.h>

@interface CarouselImageView ()<UIScrollViewDelegate>

@property (nonatomic, assign) NSInteger index;

@end

#define WIDTH [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 667
@implementation CarouselImageView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {

    }
    return self;
}

+ (instancetype)CarouselImageViewWithFrame:(CGRect)frame pageFrame:(CGRect)pageFrame imageArray:(NSMutableArray *)imageArray {
    CarouselImageView *carouselImageView = [[CarouselImageView alloc] initWithFrame:frame];
    [carouselImageView CarouselImageViewWithFrame:frame pageFrame:pageFrame imageArray:imageArray];
    
    return carouselImageView;
}

- (void)CarouselImageViewWithFrame:(CGRect)frame pageFrame:(CGRect)pageFrame imageArray:(NSMutableArray *)imageArray {
    
    /**< scrollView */
    self.scrollView = [[UIScrollView alloc] initWithFrame:frame];
    self.scrollView.backgroundColor = [UIColor blackColor];
    self.scrollView.contentSize = CGSizeMake(frame.size.width * (imageArray.count + 2), 0);
    self.scrollView.contentOffset = CGPointMake(frame.size.width, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self addSubview:self.scrollView];
    
    /**< pageControl */
    self.pageControl = [[UIPageControl alloc] initWithFrame:pageFrame];
    self.pageControl.center = CGPointMake(self.scrollView.center.x, pageFrame.origin.y);
    self.pageControl.numberOfPages = imageArray.count;
    self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    [self addSubview:self.pageControl];
    [self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
    //
    UIImageView *firstImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:[imageArray lastObject]] placeholderImage:[UIImage imageNamed:@"place"]];
    [self.scrollView addSubview:firstImage];
    
    
    UIImageView *lastImage = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width * (imageArray.count + 1), 0, frame.size.width, frame.size.height)];
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:imageArray[0]] placeholderImage:[UIImage imageNamed:@"place"]];    [self.scrollView addSubview:lastImage];
    
    
    //
    for (int i = 0; i < imageArray.count; i++) {
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width * (i + 1), 0, frame.size.width, frame.size.height)];
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:imageArray[i]] placeholderImage:[UIImage imageNamed:@"place"]];
        self.imageView.tag = 1000 + i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1; // 单击
        self.imageView.userInteractionEnabled = YES;
        [self.imageView addGestureRecognizer:tap];
        [self.scrollView addSubview:self.imageView];
        
    }
    
    
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(handleAction:) userInfo:nil repeats:YES];
}

#pragma mark - UIImageView 点击事件
- (void)tapAction:(UITapGestureRecognizer *)tap {
    if ([self.delegate respondsToSelector:@selector(didClick:)]) {
        [self.delegate didClick:tap.view.tag];
    }
}

#pragma mark - pageControl 点击事件
- (void)pageAction:(UIPageControl *)page {
    [self.scrollView setContentOffset:CGPointMake(page.currentPage * self.scrollView.frame.size.width + self.scrollView.frame.size.width, 0) animated:YES];
}

#pragma mark - 循环滑动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.pageControl.currentPage = self.scrollView.contentOffset.x / self.scrollView.frame.size.width - 1;
    if (self.scrollView.contentOffset.x <= 0) {
        self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width * (self.scrollView.contentSize.width / self.scrollView.frame.size.width - 2), 0);
        self.pageControl.currentPage = self.scrollView.contentOffset.x / self.scrollView.frame.size.width - 1;
    } else if (self.scrollView.contentOffset.x >= self.scrollView.frame.size.width * (self.scrollView.contentSize.width / self.scrollView.frame.size.width - 1)) {
        self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
        self.pageControl.currentPage = self.scrollView.contentOffset.x / self.scrollView.frame.size.width - 1;
    }
}

#pragma mark - 定时器
- (void)handleAction:(NSTimer *)timer {
    self.index++;
    [UIView animateWithDuration:0.8 animations:^{
        self.pageControl.currentPage = self.index - 1;
        self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width * self.index, 0);
        
    }completion:^(BOOL finished) {
        
    }];
    
    
    if (self.scrollView.contentOffset.x >= self.scrollView.frame.size.width * (self.scrollView.contentSize.width / self.scrollView.frame.size.width - 1)) {
        
        self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
        self.index = 1;
        
        self.pageControl.currentPage = self.index - 1;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
